<?php
namespace Common\Controller;
use Common\Controller\CommonController;
class CountLikeController extends CommonController
{

    /**
     * 缓存世界排行
     */
    public function cacherank(){

        $rank = M('countlike_member')->field('nickname,avatarurl,money,rankname')->order('money desc')->limit(100)->select();

        foreach ($rank as $key=>$value){

            $rank[$key]['money'] = $rank[$key]['money']/10000;
            $rank[$key]['money'] = $rank[$key]['money'].'万金币';
        }

        S('allrank',$rank,3600);
    }

    /**
     * 获取世界排行
     * @return mixed
     */
    public function getcacherank(){

        $cache = S('allrank');

        if (empty($cache)){
            $this->cacherank();
            $cache = S('allrank');
        }
        return $cache;
    }

    /**
     * 获取题目
     */
    public function get_question($rang_id,$nums)
    {
        $rank       =   1;
        if($rang_id >= 1 && $rang_id <= 3)
        {
            $rank   =   1;
        }
        elseif ($rang_id >= 4 && $rang_id <= 6)
        {
            $rank   =   2;
        }
        elseif ($rang_id >= 7 && $rang_id <= 12)
        {
            $rank   =   3;
        }

        $questions_data     =   S('countlike_questions');
        if (!$questions_data)
        {
            $questions_data     =   M('countlike_questions')->field('rightsum,category',true)->select();
            S('countlike_questions',$questions_data,3600*24*30);
        }

        $lsit   =   array();
        foreach ($questions_data as $key => $val)
        {
            if ($rank == $val['rank'])
            {
                $lsit[]     =   $val;
            }
        }
        $keys   =   array_rand($lsit,$nums);

        $data_last   =   array();
        foreach($keys  as $vals)
        {
            $data_last[]    =   $lsit[$vals];
        }
        return $data_last;
    }

    /**
     * 活动题目
     */
    public function activeityquestion(){

       $data = array();

        $data[] = $this->get_question(2,15);
        $data[] = $this->get_question(5,15);
        $data[] = $this->get_question(8,15);
        $data[] = $this->get_question(5,15);
        $data[] = $this->get_question(2,15);
        $data[] = $this->get_question(8,15);
        $data[] = $this->get_question(2,10);
        $data[] = $this->get_question(8,100);

       // $data[] = $this->get_question(2,5);

       foreach ($data as $key=>$val){
           foreach ($val as $va11){
               $arr[] = $va11;
           }

       }
        return $arr;

    }

    /**
     * 红包模式题目
     * @param $member_id
     * @param $time
     * @return array
     */

    public function redpacketquestion($member_id,$time,$appid){

        $data = array();

        $data[] = $this->get_question(2,4);
       $data[] = $this->get_question(5,4);
       $data[] = $this->get_question(8,2);
        foreach ($data as $key=>$val){
            foreach ($val as $va11){
                $question[$va11['countlike_questions_id']] = $va11['anwser'];
                $va11['time'] = 2;
                $arr[] = $va11;
            }

        }

        $insert = array(
            'countlike_member_id'=>$member_id,
            'create_time'=>$time,
            'anwser'=>json_encode($question),
            'appid'=>$appid,
            'state'=>0,
        );

        M('countlike_questions_played')->data($insert)->add();

        return $arr;

    }

    /**
     * 生成红包
     */
    public function create_redbag($uid,$appid,$game_id)
    {
        $where_relationship                 =   array();
        $field                              =   array('countlike_relationship_id');
        $where_relationship['my_member_id'] =   $uid;
       // $is_shere                           =   M('countlike_relationship')->where($where_relationship)->field($field)->find();

        $money_total = M('countlike_member')->where(array('countlike_member_id'=>$uid))->getField('money_total'); //当前奖金

        $where_red                  =   array();
        $where_red['member_id']     =   $uid;
        $redbag_num                 =   M('countlike_redbag')->where($where_red)->count();

        if (!empty($redbag_num))
        {
            if ($money_total<2.88){
                $red_total = rand(25,50) / 100; //奖金
                if ($money_total<0.78){
                    $red_total = rand(78,98) / 100; //奖金
                }
            }else{

                if ($money_total<3.88){
                    $red_total = $this->probability2();
                }else {
                    $create_time = M('countlike_cash')->order('id desc')->getField('create_time'); //领取时间

                    if ($create_time){
                        $whreshare['createtime'] = array('egt',$create_time);
                    }

                    $whreshare['my_member_id'] = $uid;
                    $sharenum = M('countlike_relationship')->where($whreshare)->count();

                    if ($sharenum>=7){
                        $red_total = rand(5,10) / 100;
                        if ($red_total>=10){
                            $red_total = rand(10,20) / 100;
                        }
                    }else{
                        $red_total = 0.01;
                    }
                }
            }

            //$red_total                  =   $is_shere ? rand(10,15) / 100  :  rand(10,13) / 100; //奖金
        }
        else
        {
            $red_total = rand(78,98) / 100; //奖金
        }



        $insert_data                =   array();
        $insert_data['member_id']   =   $uid;
        $insert_data['red_total']   =   number_format($red_total, 2);
        $insert_data['status']      =   0;
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['total_sn']    =   $this->create_order();
        $insert_data['time']        =   time();
        $insert_data['appid']        =   $appid;
        $insert_data['game_id']        =   $game_id;

        $result     =    M('countlike_redbag')->add($insert_data);
        if (! empty($result))
        {
            $insert_data['challenge_id']    =   $result;
            $this->create_redbag_log($insert_data);
            $this->create_change_log($insert_data);
        }
        return  $result;
    }


    //生成红包记录
    private function create_redbag_log($insert_data)
    {
        $insert_data['create_time'] =   date('Y-m-d H:i:s');
        $insert_data['time']        =   time();
        M('countlike_redbag_log')->add($insert_data);
    }

    //生成收支记录
    private  function create_change_log($insert_data)
    {
        //添加一条收入记录
        $insert_change_log                  =   array();
        $insert_change_log['game_id']       =   $insert_data['game_id'];
        $insert_change_log['member_id']     =   $insert_data['member_id'];
        $insert_change_log['red_total']     =   '+'.$insert_data['red_total'];
        $insert_change_log['status']        =   0;
        $insert_change_log['create_time']   =   $insert_data['create_time'];
        $insert_change_log['from_sn']       =   $insert_data['total_sn'];
        $insert_change_log['type_id']       =   1;
        $insert_change_log['status_text']   =   '现金红包';
        $insert_change_log['time']          =   time();
        $insert_change_log['appid']       =   $insert_data['appid'];

        M('countlike_change_log')->add($insert_change_log);
    }

    /**
     * 生成订单
     */
    private function create_order()
    {
        $order_id           =   'HB'.date('YmdHis').rand(100000,999999);
        $where              =   array();
        $field              =   array('challenge_id');
        $where['total_sn']  =   $order_id;
        $resutl             =   M('countlike_redbag')->where($where)->field($field)->find();
        if (empty($resutl))
        {
            return  $order_id;
        }
        else
        {
            $this->create_order();
        }
    }



    /**
     * 获取等级数据
     **/
    public function get_rank($rank_id = null)
    {
        $rank_data  =  S('countlike_rank');
        if (!$rank_data)
        {
            $rank_data = self::set_rank_cache();
        }

        if ($rank_id)
        {
            if ($rank_data)
            {
                foreach ($rank_data as $key => $val)
                {
                    if($val['rank'] == $rank_id)
                    {
                        return $val;
                    }
                }
                return null;
            }
        }
        return $rank_data;
    }

    /**
     * 获取游戏规则
     */
    public function get_rule()
    {
        $rank_data  =  S('countlike_rule');
        if (!$rank_data)
        {
            $rank_data   =   M('countlike_rule')->find();
            S('countlike_rule',$rank_data,3600*24*30);
        }
        return $rank_data;
    }

    private function set_rank_cache()
    {
        $data   =   M('countlike_rank')->select();
        S('countlike_rank',$data,3600*24*30);
        return $data;
    }

    public function get_sign_config($nums = 'all')
    {
        $signin_config  =  S('countlike_signin_config');
        if (empty($signin_config))
        {
            $signin_config   =   M('countlike_signin_config')->find();
            S('countlike_signin_config',$signin_config,3600*24*30);
        }

        switch ($nums)
        {
            case 1:
                return $signin_config['sign_one'];
                break;

            case 2:
                return $signin_config['sign_two'];
                break;

            case 3:
                return $signin_config['sign_three'];
                break;

            case 4:
                return $signin_config['sign_four'];
                break;

            case 5:
                return $signin_config['sign_five'];
                break;

            case 6:
                return $signin_config['sign_six'];
                break;

            case 7:
                return $signin_config['sign_sever'];
                break;

            case 'all':
                return $signin_config;
                break;
        }

    }

//    /**
//     * 获取排行
//     */
//    public function getrank($date,$state){
//
//        $cache = S('activity_countlikerank');
//
//        if ($state!='2'){
//            if (!empty($cache)){
//                 return $cache;
//            }
//        }
//
//        if (empty($date)){
//            $date = date('Y-m-d');
//        }
//
//        $ids = M('countlike_change')->where(array('date'=>$date))->getField('countlike_member_id',true);
//        $data = array();
//        if (!empty($ids)){
//            $ids = array_unique($ids);  //去除重复值
//            $ids = array_merge($ids);  //去下标
//            for ($i=0;$i<count($ids);$i++){
//
//                $where['date'] = $date;
//                $where['countlike_member_id'] = $ids[$i];
//                $info = M('countlike_change')->field('id,countlike_member_id,week,year,createtime',true)->where($where)->order('num desc')->find();
//                $data[] = $info;
//            }
//            //冒泡
//            $len = count($data);
//            for ($k=0;$k<$len;$k++){
//                for ($j=0;$j<$len-$k;$j++){
//                    if ($data[$j]['num']<$data[$j+1]['num']){
//                        $tem = $data[$j];
//                        $data[$j] = $data[$j+1];
//                        $data[$j+1] = $tem;
//                    }
//                }
//
//            }
//            if ($state=='2'){
//
//                if ($data[0]){
//                    $data[0]['giftname'] = '获得奖金200元';
//                }
//                if ($data[1]){
//                    $data[1]['giftname'] = '获得奖金100元';
//                }
//                if ($data[2]){
//                    $data[2]['giftname'] = '获得奖金50元';
//                }
//
//                $arr['gift'] = array(
//                    '0'=>$data[0],
//                    '1'=>$data[1],
//                    '2'=>$data[2],
//                );
//                $m = date('m',strtotime($date));
//                $d = date('d',strtotime($date));
//                $arr['date'] = (int)$m.'月'.(int)$d.'日';
//                return $arr;
//            }
//
//            if ($data[0]){
//                $data[0]['giftname'] = '预计获得奖金200元';
//            }
//            if ($data[1]){
//                $data[1]['giftname'] = '预计获得奖金100元';
//            }
//            if ($data[2]){
//                $data[2]['giftname'] = '预计获得奖金50元';
//            }
//
//
//        }
//
//        S('activity_countlikerank',$data,600);
//        return S('activity_countlikerank');
//
//    }

    /**
     * 获取排行
     */
    public function getrank($date,$state){

        $cache = S('activity_countlikerank');

        if ($state!='2'){
            if (!empty($cache)){
               return $cache;
            }
        }
        if (empty($date)){
            $date = date('Y-m-d');
        }
        if ($state=='2')
        {
            $data  =   M('countlike_change')->where(array('date'=>$date))->field("max(num) as num,nickname,avatarurl,date")->group('countlike_member_id')->order(array('num'=>'desc'))->limit(3)->select();
            if ($data[0]){
                $data[0]['giftname'] = '获得奖金200元';
            }
            if ($data[1]){
                $data[1]['giftname'] = '获得奖金100元';
            }
            if ($data[2]){
                $data[2]['giftname'] = '获得奖金50元';
            }

            $arr['gift'] = array(
                '0'=>$data[0],
                '1'=>$data[1],
                '2'=>$data[2],
            );
            $m = date('m',strtotime($date));
            $d = date('d',strtotime($date));
            $arr['date'] = (int)$m.'月'.(int)$d.'日';
            return $arr;

        }
        $data  =   M('countlike_change')->where(array('date'=>$date))->field("max(num) as num,nickname,avatarurl,date")->group('countlike_member_id')->order(array('num'=>'desc'))->limit(100)->select();

//        if ($data[0]){
//                $data[0]['giftname'] = '预计获得奖金200元';
//            }
//            if ($data[1]){
//                $data[1]['giftname'] = '预计获得奖金100元';
//            }
//            if ($data[2]){
//                $data[2]['giftname'] = '预计获得奖金50元';
//            }

        S('activity_countlikerank',$data,600);
        return S('activity_countlikerank');

    }



    private function moni($date){

        $data = array();
        $arr[0] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/sstounao..jpg',
            'giftname'=>'获得奖金200元'
        );
        $arr[1] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/sstounao..jpg',
            'giftname'=>'获得奖金100元'
        );
        $arr[2] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/sstounao..jpg',
            'giftname'=>'获得奖金50元'
        );
        $data['gift'] = array(
            '0'=>$arr[0],
            '1'=>$arr[1],
            '2'=>$arr[2],
        );

        $m = date('m',strtotime($date));
        $d = date('d',strtotime($date));
        $data['date'] = (int)$m.'月'.(int)$d.'日';
        return $data;
    }



    /**
     * 近期得奖
     */
    public function gift(){


        $date1 = '2018-06-16';
        $date2 = '2018-06-17';
        $date3 = '2018-06-18';
        $noedate = date('Y-m-d',time());
        $state = 2;

        if ($noedate>$date1){
            $data[0] = $this->getrank($date1,$state);
        }else{
            $data[0] = $this->moni($date1);

        }
        if ($noedate>$date2){
            $data[1] = $this->getrank($date2,$state);
        }else{
            $data[1] = $this->moni($date2);
        }

        if ($noedate>$date3){
            $data[2] = $this->getrank($date3,$state);
        }else{
            $data[2] = $this->moni($date3);
        }

        return $data;

    }

}